package cn.kai.org.controller;

import cn.kai.basic.anno.LogAnno;
import cn.kai.basic.anno.PreAuthorize;
import cn.kai.basic.config.BussinessException;
import cn.kai.basic.query.BaseQuery;
import cn.kai.basic.util.AjaxResult;
import cn.kai.basic.util.LogContext;
import cn.kai.basic.util.PageList;
import cn.kai.org.domain.Employee;
import cn.kai.org.service.IEmployeeServiceI;
import cn.kai.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description = "员工相关的CRUD功能")
@PreAuthorize
public class EmployeeController{

    @Autowired
    private IEmployeeServiceI employeeService;

    /**
     * 添加/修改
     */
    @PutMapping
    @PreAuthorize(name = "员工添加和修改",sn = "employee:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (null == employee.getId()){
                // 添加
                employeeService.add(employee);
            }else {
                // 修改
                employeeService.update(employee);
            }
            return AjaxResult.me().success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请重试!");
        }
    }

    /**
     * 删除 delete localhost:8080/dept/1
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "员工删除",sn = "employee:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.me().success();
        }catch (BussinessException bu){
            bu.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(bu.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请重试!");
        }
    }

    /**
     * 查询所有
     */
    @GetMapping
    @PreAuthorize(name = "查询所有部门",sn = "employee:findAll")
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public Employee findById(@ApiParam(name="主键",required = true) @PathVariable("id") Long id){
        return (Employee) employeeService.findById(id);
    }

    /**
     *
     * 分页查询
     */
    @PostMapping
    @PreAuthorize(name = "员工查询所有",sn = "employee:queryByPage")
    public PageList<Employee> queryByPage(@RequestBody BaseQuery query){
        return employeeService.queryByPage(query);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            employeeService.batchDel(ids);
            return AjaxResult.me().success();
        }catch (BussinessException bu){
            bu.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(bu.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请重试!");
        }
    }

    /**
     * 根据shopId查询所有的员工，这里的shopId通过当前登录人的shopId获得
     * @return
     */
    @GetMapping("/list")
    public List<Employee> list(HttpServletRequest request){
        Logininfo logininfo = LogContext.getLogininfo(request);
        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
        Long shopId = employee.getShopId();
        if (shopId == null){
            throw new BussinessException("当前登录人员不是店铺员工，请重新登录！");
        }
        return employeeService.list(shopId);
    }
}
